package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.*;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);

        if(queryString != null && !"".equals(queryString)){
            int level = menuDao.findLevelByCondition(queryString);
            if(level == 1){
                Page<Menu> pages = menuDao.findByCondition(queryString);
                long total = pages.getTotal();
                return new PageResult(total, pages);
            }else{
                Page<Menu> pages = menuDao.findByQueryStringAndLevel(queryString);
                long total = pages.getTotal();
                return new PageResult(total, pages);
            }
        }else{
            Page<Menu> pages = menuDao.findByCondition(queryString);
            long total = pages.getTotal();
            return new PageResult(total, pages);
        }
    }

    // 展示 所有 menu
    @Override
    public List<Map<String, Object>> findMenuparentMenuId() {
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("label", "一级菜单");
        map1.put("value", null);
        maps.add(map1);

        List<Menu> list = menuDao.findByCondition(null);
        for (Menu menu : list) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("label", menu.getName());
            map.put("value", menu.getId());

            List<Menu> children = menu.getChildren();
            ArrayList<Object> maps_c = new ArrayList<>();
            if (children != null && children.size() > 0) {
                for (Menu child : children) {
                    HashMap<String, Object> map_c = new HashMap<>();
                    map_c.put("label", child.getName());
                    map_c.put("value", child.getId());
                    maps_c.add(map_c);
                }
            }
            map.put("children", maps_c);
            maps.add(map);
        }
        return maps;
    }

    @Override
    public void add(Integer parentMenuId, Menu menu) {
        if (parentMenuId != 9999) {
            menu.setParentMenuId(parentMenuId);
        }
        menuDao.add(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public List<Map<String, Object>> findMenuIdAndDisabled(Integer id) {
        return menuDao.findMenuIdAndDisabled(id);
    }

    @Override
    public void edit(Integer id, Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void delete(Integer id) {
        // 解除表关系
        menuDao.deleteMenuIdAndRoleId(id);
        menuDao.delete(id);
    }

    @Override
    public List<Map<String, Object>> findMenuList(String username) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        HashMap<String, Object> map_1 = new HashMap<>();
        map_1.put("path", 1);
        map_1.put("title", "工作台");
        map_1.put("icon", "fa-dashboard");
        list.add(map_1);

        LinkedHashSet<Menu> menus = menuDao.findMenuList(username);

        for (Menu menu : menus) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("path", menu.getPath());
            map.put("title", menu.getName());
            map.put("icon", menu.getIcon());
            ArrayList<Map<String, Object>> list_c = new ArrayList<>();
            List<Menu> children = menu.getChildren();
            if (children != null && children.size() > 0) {
                for (Menu child : children) {
                    HashMap<String, Object> map_c = new HashMap<>();
                    map_c.put("path", child.getPath());
                    map_c.put("title", child.getName());
                    map_c.put("linkUrl", child.getLinkUrl());
                    list_c.add(map_c);
                }
            }
            map.put("children", list_c);
            list.add(map);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findAllMenu() {

        /*装所有的 menumap*/
        ArrayList<Map<String, Object>> list = new ArrayList<>();

        Page<Menu> menus = menuDao.findByCondition(null);
        for (Menu menu : menus) {


            HashMap<String, Object> map = new HashMap<>();
            map.put("id", menu.getId());
            map.put("label", menu.getName());

            ArrayList<Map<String, Object>> list_c = new ArrayList<>();
            List<Menu> children = menu.getChildren();
            if (children != null && children.size() > 0) {
                for (Menu child : children) {
                    HashMap<String, Object> map_c = new HashMap<>();
                    map_c.put("id", child.getId());
                    map_c.put("label", child.getName());
                    list_c.add(map_c);
                }
            }
            map.put("children", list_c);
            list.add(map);
        }

        return list;
    }

    @Override
    public List<Integer> findMenuCheckedByRoleId(Integer roleId) {
        return menuDao.findMenuCheckedByRoleId(roleId);
    }

}
